<!--
 * @Description: 任务详情组件
-->
<template>
    <el-dialog
      v-model="dialogVisible"
      title="任务详情"
      width="700px"
      :close-on-click-modal="false"
      @close="handleClose"
    >
      <div class="task-detail" v-if="task">
        <!-- 任务头部信息 -->
        <div class="task-header">
          <div class="title-section">
            <h3 class="task-title">{{ task.title }}</h3>
            <div class="task-meta">
              <el-tag 
                :type="getStatusType(task.status)" 
                size="large"
                class="status-tag"
              >
                {{ getStatusText(task.status) }}
              </el-tag>
              <el-tag 
                :type="getPriorityType(task.priority)" 
                size="large"
                class="priority-tag"
              >
                <el-icon v-if="task.priority === 'urgent'"><Warning /></el-icon>
                <el-icon v-else-if="task.priority === 'high'"><ArrowUp /></el-icon>
                <el-icon v-else-if="task.priority === 'medium'"><Minus /></el-icon>
                <el-icon v-else><ArrowDown /></el-icon>
                {{ getPriorityText(task.priority) }}
              </el-tag>
            </div>
          </div>
        </div>
  
        <!-- 基本信息卡片 -->
        <el-card class="info-card" shadow="never">
          <template #header>
            <div class="card-header">
              <el-icon><InfoFilled /></el-icon>
              <span>基本信息</span>
            </div>
          </template>
          
          <el-row :gutter="20">
            <el-col :span="12">
              <div class="info-item">
                <label>所属部门：</label>
                <span>{{ task.department }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="info-item">
                <label>任务类型：</label>
                <span>{{ task.taskType }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="info-item">
                <label>创建时间：</label>
                <span>{{ formatDate(task.createdAt) }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="info-item">
                <label>截止时间：</label>
                <span :class="getDeadlineClass(task.deadline, task.status)">
                  {{ formatDate(task.deadline) }}
                  <el-tag 
                    v-if="getDeadlineStatus(task.deadline, task.status)" 
                    :type="getDeadlineTagType(task.deadline, task.status)"
                    size="small"
                    class="deadline-status"
                  >
                    {{ getDeadlineStatus(task.deadline, task.status) }}
                  </el-tag>
                </span>
              </div>
            </el-col>
          </el-row>
        </el-card>
  
        <!-- 分配人员卡片 -->
        <el-card class="info-card" shadow="never">
          <template #header>
            <div class="card-header">
              <el-icon><User /></el-icon>
              <span>分配人员</span>
            </div>
          </template>
          
          <div class="assigned-personnel">
            <div 
              v-for="name in task.assignedToNames" 
              :key="name"
              class="personnel-item"
            >
              <el-avatar :size="40" class="avatar">
                {{ name.charAt(0) }}
              </el-avatar>
              <div class="personnel-info">
                <div class="name">{{ name }}</div>
                <div class="department">{{ getPersonnelDepartment(name) }}</div>
                <div class="nature">{{ getPersonnelNature(name) }}</div>
              </div>
            </div>
          </div>
        </el-card>
  
        <!-- 任务描述卡片 -->
        <el-card class="info-card" shadow="never">
          <template #header>
            <div class="card-header">
              <el-icon><Document /></el-icon>
              <span>任务描述</span>
            </div>
          </template>
          
          <div class="task-description">
            {{ task.description }}
          </div>
        </el-card>
  
        <!-- 备注信息卡片 -->
        <el-card v-if="task.remarks" class="info-card" shadow="never">
          <template #header>
            <div class="card-header">
              <el-icon><ChatDotRound /></el-icon>
              <span>备注信息</span>
            </div>
          </template>
          
          <div class="task-remarks">
            {{ task.remarks }}
          </div>
        </el-card>
  
        <!-- 附件信息卡片 -->
        <el-card v-if="task.attachments && task.attachments.length > 0" class="info-card" shadow="never">
          <template #header>
            <div class="card-header">
              <el-icon><Paperclip /></el-icon>
              <span>附件信息</span>
            </div>
          </template>
          
          <div class="attachments">
            <div 
              v-for="(attachment, index) in task.attachments" 
              :key="index"
              class="attachment-item"
            >
              <el-icon class="file-icon"><Document /></el-icon>
              <span class="file-name">附件{{ index + 1 }}</span>
              <el-button type="primary" size="small" text @click="downloadAttachment(attachment)">
                下载
              </el-button>
            </div>
          </div>
        </el-card>
  
        <!-- 时间轴 -->
        <el-card class="info-card" shadow="never">
          <template #header>
            <div class="card-header">
              <el-icon><Clock /></el-icon>
              <span>任务时间轴</span>
            </div>
          </template>
          
          <el-timeline>
            <el-timeline-item
              timestamp="创建时间"
              :hollow="true"
              type="primary"
            >
              任务创建于 {{ formatDateTime(task.createdAt) }}
            </el-timeline-item>
            
            <el-timeline-item
              v-if="task.status !== 'pending'"
              timestamp="开始时间"
              :hollow="true"
              type="success"
            >
              任务开始执行
            </el-timeline-item>
            
            <el-timeline-item
              v-if="task.status === 'completed'"
              timestamp="完成时间"
              :hollow="true"
              type="success"
            >
              任务已完成
            </el-timeline-item>
            
            <el-timeline-item
              v-if="task.status === 'cancelled'"
              timestamp="取消时间"
              :hollow="true"
              type="danger"
            >
              任务已取消
            </el-timeline-item>
            
            <el-timeline-item
              v-if="task.status !== 'completed' && task.status !== 'cancelled'"
              timestamp="预计完成"
              :hollow="true"
              :type="getTimelineType(task.deadline, task.status)"
            >
              预计 {{ formatDate(task.deadline) }} 完成
            </el-timeline-item>
          </el-timeline>
        </el-card>
      </div>
      
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">关闭</el-button>
          <el-button type="primary" @click="handleEdit">
            <el-icon><Edit /></el-icon>
            编辑任务
          </el-button>
        </div>
      </template>
    </el-dialog>
  </template>
  
  <script setup lang="ts">
  import { computed, inject } from 'vue';
  import { ElMessage } from 'element-plus';
  import { 
    InfoFilled,
    User,
    Document,
    ChatDotRound,
    Paperclip,
    Clock,
    Edit,
    Warning,
    ArrowUp,
    ArrowDown,
    Minus
  } from '@element-plus/icons-vue';
  
  // 接口定义
  interface PersonnelInfo {
    index: number;
    personnelCategory: string;
    department: string;
    name: string;
    gender: string;
    education: string;
    degree: string;
    graduatedFrom: string;
    personnelNature: string;
    workContent: string;
    workLocation: string;
    entryDate: string;
    contractStartDate: string;
    contractEndDate: string;
  }
  
  interface TaskInfo {
    id?: string;
    title: string;
    description: string;
    assignedTo: string[];
    assignedToNames: string[];
    status: 'pending' | 'in-progress' | 'completed' | 'cancelled';
    priority: 'low' | 'medium' | 'high' | 'urgent';
    createdAt?: string;
    deadline: string;
    department: string;
    taskType: string;
    attachments?: string[];
    remarks?: string;
  }
  
  // Props 和 Emits
  const props = defineProps<{
    modelValue: boolean;
    task?: TaskInfo | null;
    personnelList?: PersonnelInfo[];
  }>();
  
  const emit = defineEmits<{
    'update:modelValue': [value: boolean];
    close: [];
    edit: [task: TaskInfo];
  }>();
  
  // 计算属性
  const dialogVisible = computed({
    get: () => props.modelValue,
    set: (value) => emit('update:modelValue', value)
  });
  
  // 工具方法
  const getStatusType = (status: string) => {
    const statusMap = {
      'pending': 'warning',
      'in-progress': 'primary',
      'completed': 'success',
      'cancelled': 'danger'
    };
    return statusMap[status as keyof typeof statusMap] || 'info';
  };
  
  const getStatusText = (status: string) => {
    const statusMap = {
      'pending': '待处理',
      'in-progress': '进行中',
      'completed': '已完成',
      'cancelled': '已取消'
    };
    return statusMap[status as keyof typeof statusMap] || status;
  };
  
  const getPriorityType = (priority: string) => {
    const priorityMap = {
      'urgent': 'danger',
      'high': 'warning',
      'medium': 'primary',
      'low': 'info'
    };
    return priorityMap[priority as keyof typeof priorityMap] || 'info';
  };
  
  const getPriorityText = (priority: string) => {
    const priorityMap = {
      'urgent': '紧急',
      'high': '高',
      'medium': '中',
      'low': '低'
    };
    return priorityMap[priority as keyof typeof priorityMap] || priority;
  };
  
  const formatDate = (dateStr?: string) => {
    if (!dateStr) return '';
    return new Date(dateStr).toLocaleDateString('zh-CN');
  };
  
  const formatDateTime = (dateStr?: string) => {
    if (!dateStr) return '';
    return new Date(dateStr).toLocaleString('zh-CN');
  };
  
  const getDeadlineClass = (deadline: string, status: string) => {
    if (status === 'completed' || status === 'cancelled') {
      return '';
    }
    
    const now = new Date();
    const deadlineDate = new Date(deadline);
    const diffDays = Math.ceil((deadlineDate.getTime() - now.getTime()) / (1000 * 60 * 60 * 24));
    
    if (diffDays < 0) {
      return 'deadline-overdue';
    } else if (diffDays <= 3) {
      return 'deadline-warning';
    }
    return '';
  };
  
  const getDeadlineStatus = (deadline: string, status: string) => {
    if (status === 'completed' || status === 'cancelled') {
      return '';
    }
    
    const now = new Date();
    const deadlineDate = new Date(deadline);
    const diffDays = Math.ceil((deadlineDate.getTime() - now.getTime()) / (1000 * 60 * 60 * 24));
    
    if (diffDays < 0) {
      return '已逾期';
    } else if (diffDays <= 3) {
      return '即将到期';
    }
    return '';
  };
  
  const getDeadlineTagType = (deadline: string, status: string) => {
    if (status === 'completed' || status === 'cancelled') {
      return 'info';
    }
    
    const now = new Date();
    const deadlineDate = new Date(deadline);
    const diffDays = Math.ceil((deadlineDate.getTime() - now.getTime()) / (1000 * 60 * 60 * 24));
    
    if (diffDays < 0) {
      return 'danger';
    } else if (diffDays <= 3) {
      return 'warning';
    }
    return 'info';
  };
  
  const getTimelineType = (deadline: string, status: string) => {
    if (status === 'completed') return 'success';
    if (status === 'cancelled') return 'danger';
    
    const now = new Date();
    const deadlineDate = new Date(deadline);
    const diffDays = Math.ceil((deadlineDate.getTime() - now.getTime()) / (1000 * 60 * 60 * 24));
    
    if (diffDays < 0) return 'danger';
    if (diffDays <= 3) return 'warning';
    return 'primary';
  };
  
  const getPersonnelDepartment = (name: string) => {
    if (!props.personnelList) return '';
    const person = props.personnelList.find(p => p.name === name);
    return person?.department || '';
  };
  
  const getPersonnelNature = (name: string) => {
    if (!props.personnelList) return '';
    const person = props.personnelList.find(p => p.name === name);
    return person?.personnelNature || '';
  };
  
  // 事件处理
  const handleClose = () => {
    emit('close');
  };
  
  const handleEdit = () => {
    if (props.task) {
      emit('edit', props.task);
    }
  };
  
  const downloadAttachment = (url: string) => {
    if (url) {
      window.open(url, '_blank');
    } else {
      ElMessage.warning('附件链接无效');
    }
  };
  </script>
  
  <style lang="scss" scoped>
  .task-detail {
    .task-header {
      margin-bottom: 20px;
      
      .title-section {
        .task-title {
          margin: 0 0 12px 0;
          color: var(--el-text-color-primary);
          font-size: 24px;
          font-weight: bold;
        }
        
        .task-meta {
          display: flex;
          gap: 12px;
          align-items: center;
          
          .status-tag, .priority-tag {
            display: flex;
            align-items: center;
            gap: 4px;
          }
        }
      }
    }
    
    .info-card {
      margin-bottom: 16px;
      
      .card-header {
        display: flex;
        align-items: center;
        gap: 8px;
        font-weight: bold;
        color: var(--el-text-color-primary);
      }
      
      .info-item {
        margin-bottom: 12px;
        display: flex;
        align-items: center;
        
        label {
          font-weight: bold;
          color: var(--el-text-color-regular);
          margin-right: 8px;
          min-width: 80px;
        }
        
        span {
          color: var(--el-text-color-primary);
          display: flex;
          align-items: center;
          gap: 8px;
          
          .deadline-status {
            margin-left: 8px;
          }
        }
      }
    }
    
    .assigned-personnel {
      display: flex;
      flex-wrap: wrap;
      gap: 16px;
      
      .personnel-item {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 12px;
        border: 1px solid var(--el-border-color-light);
        border-radius: 8px;
        background: var(--el-bg-color-page);
        
        .avatar {
          background: var(--el-color-primary);
          color: white;
        }
        
        .personnel-info {
          .name {
            font-weight: bold;
            color: var(--el-text-color-primary);
            margin-bottom: 4px;
          }
          
          .department {
            font-size: 12px;
            color: var(--el-text-color-regular);
            margin-bottom: 2px;
          }
          
          .nature {
            font-size: 12px;
            color: var(--el-color-primary);
            background: var(--el-color-primary-light-9);
            padding: 2px 6px;
            border-radius: 4px;
            display: inline-block;
          }
        }
      }
    }
    
    .task-description {
      line-height: 1.6;
      color: var(--el-text-color-primary);
      white-space: pre-wrap;
    }
    
    .task-remarks {
      line-height: 1.6;
      color: var(--el-text-color-regular);
      white-space: pre-wrap;
      background: var(--el-bg-color-page);
      padding: 12px;
      border-radius: 6px;
    }
    
    .attachments {
      display: flex;
      flex-direction: column;
      gap: 8px;
      
      .attachment-item {
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 8px 12px;
        border: 1px solid var(--el-border-color-light);
        border-radius: 6px;
        background: var(--el-bg-color-page);
        
        .file-icon {
          color: var(--el-color-primary);
        }
        
        .file-name {
          flex: 1;
          color: var(--el-text-color-primary);
        }
      }
    }
  }
  
  .dialog-footer {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
  }
  
  // 截止时间样式
  .deadline-overdue {
    color: var(--el-color-danger) !important;
    font-weight: bold;
  }
  
  .deadline-warning {
    color: var(--el-color-warning) !important;
    font-weight: bold;
  }
  
  // 响应式设计
  @media (max-width: 768px) {
    .assigned-personnel {
      .personnel-item {
        width: 100%;
      }
    }
    
    .attachments {
      .attachment-item {
        flex-direction: column;
        align-items: flex-start;
      }
    }
  }
  </style>